package org.lhz;

import com.alibaba.fastjson.JSON;
import org.lhz.dao.BunsCarMessageDao;
import org.lhz.dao.impl.BunsCarMessageDaoImpl;
import org.lhz.entity.BunsBusCar;
import org.lhz.entity.BunsBusCustomer;
import org.lhz.entity.BunsBusRent;
import org.lhz.service.BunsCarMessageService;
import org.lhz.service.BunsCarRentalService;
import org.lhz.service.impl.BunsCarMessageServiceImpl;
import org.lhz.service.impl.BunsCarRentalServiceImpl;
import org.lhz.vo.BunsBusCarVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@WebServlet("/foreSystem/car")
public class CarController extends HttpServlet {
    //注入service层对象
    private BunsCarMessageService bunsCarMessageService = new BunsCarMessageServiceImpl();
    private BunsCarRentalService bunsCarRentalService = new BunsCarRentalServiceImpl();
    //调用dao层对象
    private BunsCarMessageDao bunsCarMessageDao = new BunsCarMessageDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        //根据请求参数分别处理请求
        switch (method) {
            case"loadCarType":
                loadCarType(req, resp);
                break;
            case"listAll":
                listAll(req, resp);
                break;
            case"carListByType":
                carListByType(req,resp);
                break;
            case"detail":
                detail(req,resp);
                break;
            case"addOrder":
                addOrder(req,resp);
                break;
        }
    }


    /**
     * Infor: 根据车型查询到对应的车型信息
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/7 17:57
     */
    private void carListByType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        String type = req.getParameter("type");
        BunsBusCar bunsBusCar = new BunsBusCar();
        bunsBusCar.setCartype(type);
        List<BunsBusCar> carListByType = bunsCarMessageService.findCarListByType(bunsBusCar);
        req.setAttribute("carList",carListByType);
        req.setAttribute("typeId",type);
        //请求转发过去
        req.getRequestDispatcher("/WEB-INF/view/carList.jsp").forward(req,resp);

    }


    /**
     * Infor: 导入所有的车辆信息
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/7 17:08
     */
    private void listAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String method = req.getParameter("method");
        //查询全部
        BunsBusCarVo bunsBusCarVo = new BunsBusCarVo();
        List<BunsBusCar> bunsBusCars = bunsCarMessageService.findAllCanRent(bunsBusCarVo);
        req.setAttribute("carList",bunsBusCars);
        //请求转发过去
        req.getRequestDispatcher("/WEB-INF/view/carList.jsp").forward(req,resp);
    }

    /**
     * Infor: 找到所有的车辆类型
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/7 13:55
     */
    private void loadCarType(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        List<BunsBusCar> carType = bunsCarMessageService.findCarType();
        //输出数据
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(carType));
        writer.close();

    }
    /**
     * Infor: 查询具体的车辆信息
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/8 0:27
     */
    private void detail(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        String carnumber = req.getParameter("carnumber");
        BunsBusCar bunsBusCar = new BunsBusCar();
        bunsBusCar.setCarnumber(carnumber);
        BunsBusCar carListByCarNumber = bunsCarMessageService.findCarListByCarNumber(bunsBusCar);
        //请求转发到下单页面
        req.setAttribute("carByCarNumber",carListByCarNumber);
        req.getRequestDispatcher("/WEB-INF/view/detail.jsp").forward(req,resp);
    }

    /**
     * Infor: 添加订单
     * @param req
     * @param resp
     * @return : void
     * @author : LHZ
     * @date : 2021/11/8 9:18
     */
    private void addOrder(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        //获取参数
        String carnumber = req.getParameter("carnumber");
        String arriveData = req.getParameter("arriveDate");
        String leaveDate = req.getParameter("leaveDate");
        String reservePrice = req.getParameter("reservePrice");
        String idCard = req.getParameter("idCard");
        BunsBusRent bunsBusRent = new BunsBusRent();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        bunsBusRent.setCarnumber(carnumber);
        try {
            bunsBusRent.setBegindate(sdf.parse(arriveData));
            bunsBusRent.setReturndate(sdf.parse(leaveDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        bunsBusRent.setIdentity(idCard);
        bunsBusRent.setRentflag(1);
        bunsBusRent.setCreatetime(new Date());
        bunsBusRent.setPrice(Double.valueOf(reservePrice));
        bunsBusRent.setRentid(createRent());
        HashMap<String, Object> map = new HashMap<>();
        if (bunsCarRentalService.addForeOrder(bunsBusRent)>0) {
            BunsBusCar bunsBusCar = new BunsBusCar();
            bunsBusCar.setIsrenting(1);
            //更新车辆的出租状态
            bunsCarMessageDao.updateRentStatus();//设置为1，已出租
            map.put("success",true);
            map.put("message","恭喜！下单成功！");
        }else{
            map.put("success",false);
            map.put("message","对不起！下单失败！");
        }
        //写出数据
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(JSON.toJSONString(map));

    }

    /**
     * Infor: 生成出租单号的方法
     * @param
     * @return : java.lang.String
     * @author : LHZ
     * @date : 2021/11/8 9:39
     */
    private String createRent(){
        //获取当前系统日期
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); //  将日期格式设为yyyyMMdd，填入出租单号
//        生成出租单号“CZ_当前日期_10000~99999之间的随机数“
        Boolean aBoolean = false;
        String singleNo = null;
        do {
            singleNo = "CZ_" + dateFormat.format(date.getTime()) + "_" + (int) Math.floor(Math.random() * 89999 + 10000);
            aBoolean = bunsCarRentalService.checkSingleNo(singleNo);
        } while (aBoolean);
        return singleNo;
    }
}
